package com.smcdd.cdb.dbo.cdb_dbo.ui.adapter;

import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.smcdd.cdb.dbo.cdb_dbo.R;
import com.smcdd.cdb.dbo.cdb_dbo.service.entity.Function;
import com.smcdd.cdb.dbo.cdb_dbo.service.entity.Functions;
import com.smcdd.cdb.dbo.cdb_dbo.ui.widget.HintDialog;

import java.util.ArrayList;
import java.util.List;

/**
 * 作者：Administrator on 2017/6/20 14:20
 * 邮箱：zhanghuaiha@gmail.com
 */

public class FunctionRecyclerAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {
    private Context mContext;
    private OnItemClickListenter onItemClickListenter;
    private List<Function> mList;
    /**
     * 布局类型的数量
     */
    private final int VIWE_TYPE_COUNT = 2;
    private final int TYPE_ONE = 0;
    private final int TYPE_TWO = 1;
    private HintDialog selfDialog;
    private int type = 0;

    public FunctionRecyclerAdapter(Context context){
        mContext = context;
        mList = new ArrayList<>();
    }

    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        if (viewType == TYPE_TWO) {
            return new Item2ViewHolder(LayoutInflater.from(parent.getContext()).inflate(R.layout.item_picture_two,parent,false));
        }else {
            return new Item1ViewHolder(LayoutInflater.from(parent.getContext()).inflate(R.layout.item_picture,parent,false));

        }
    }

    @Override
    public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {
        if (holder instanceof Item1ViewHolder) {
            if (mList.get(position).getSmall_type().equals("1")) {
                ((Item1ViewHolder) holder).wenhao.setVisibility(View.VISIBLE);
            }else {
                ((Item1ViewHolder) holder).wenhao.setVisibility(View.GONE);
            }
            ((Item1ViewHolder) holder).mTextView.setText(mList.get(position).getName());
            Glide.with(mContext).load(mList.get(position).getIcon()).into(((Item1ViewHolder) holder).image);
            ((Item1ViewHolder) holder).itemView.setTag(position);
            ((Item1ViewHolder) holder).itemView.setOnClickListener(v -> {
                onItemClickListenter.itemClickListenter(mList.get(position),position,mList.get(position).getCode());
            });
            ((Item1ViewHolder) holder).wenhao.setOnClickListener(v -> {
                Dialog(mList.get(position).getSmall_prompt() == null? "":(String)mList.get(position).getSmall_prompt());
            });
        } else if (holder instanceof Item2ViewHolder) {
            if (mList.get(position).getSmall_type().equals("1")) {
                ((Item2ViewHolder) holder).wenhao.setVisibility(View.VISIBLE);
            }else {
                ((Item2ViewHolder) holder).wenhao.setVisibility(View.GONE);
            }
            ((Item2ViewHolder) holder).title.setText(mList.get(position).getName());
            if (type == 1) {
                ((Item2ViewHolder) holder).two.setText("五分钟");
            }else if (type == 2) {
                ((Item2ViewHolder) holder).two.setText("wifi专用");
            }

            Glide.with(mContext).load(mList.get(position).getIcon()).into(((Item2ViewHolder) holder).image);
            ((Item2ViewHolder) holder).itemView.setTag(position);
            ((Item2ViewHolder) holder).itemView.setOnClickListener(v -> {
                onItemClickListenter.itemClickListenter(mList.get(position),position,mList.get(position).getCode());
            });
            ((Item2ViewHolder) holder).wenhao.setOnClickListener(v -> {
                Dialog(mList.get(position).getSmall_prompt() == null? "":(String)mList.get(position).getSmall_prompt());
            });
        }
    }

    private void Dialog(String a) {
        selfDialog = new HintDialog(mContext);
        selfDialog.setTitle("提示");
        selfDialog.setMessage(a);
        selfDialog.show();
    }


    @Override
    public int getItemViewType(int position) {
        if (mList != null) {
            if (mList.get(position).getName().equals("扫码充电")) {
                type = 1;
                return TYPE_TWO;
            }else if (mList.get(position).getName().equals("灯光控制")) {
                type = 2;
                return TYPE_TWO;
            }else if (mList.get(position).getName().equals("硬件升级")) {
                type = 2;
                return TYPE_TWO;
            }else if (mList.get(position).getName().equals("配置WiFi")) {
                type = 2;
                return TYPE_TWO;
            }else {
                return TYPE_ONE;
            }

        }
        return TYPE_ONE;
    }



    @Override
    public int getItemCount() {
        return mList == null? 0 : mList.size();
    }


    //item1 的ViewHolder
    public static class Item1ViewHolder extends RecyclerView.ViewHolder{
        TextView mTextView;
        ImageView image,wenhao;
        public Item1ViewHolder(View itemView) {
            super(itemView);
            mTextView=(TextView)itemView.findViewById(R.id.text_item);
            image=(ImageView)itemView.findViewById(R.id.image_item);
            wenhao=(ImageView)itemView.findViewById(R.id.image_wenhao);
        }
    }
    //item2 的ViewHolder
    public static class Item2ViewHolder extends RecyclerView.ViewHolder{
        public TextView title;
        public TextView two;
        public ImageView image,wenhao;
        public Item2ViewHolder(View itemView) {
            super(itemView);
            title=(TextView)itemView.findViewById(R.id.text_item);
            two=(TextView)itemView.findViewById(R.id.text_two);
            image=(ImageView)itemView.findViewById(R.id.image_item);
            wenhao=(ImageView)itemView.findViewById(R.id.image_wenhao);
        }
    }

    public void addData(List<Function> scan){
        if (mList != null) {
            mList.clear();
            mList.addAll(scan);
        }
        notifyDataSetChanged();
    }

    public interface OnItemClickListenter {
        void itemClickListenter(Function scan, int position,String code);
    }

    public void setItemClickListenter(OnItemClickListenter itemClickListenter) {
        onItemClickListenter = itemClickListenter;
    }

}
